﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SFWS.XHandler.Core;
using JKF.Substructure;
using JKF.Entities;
using JKF.BusinessLayer;
using JKF.Entities.Model;
namespace JKF.HandlerLayer
{
    /// <summary>
    /// 用于统一添加，修改分流
    /// 最后修改时间2010-05-23
    /// </summary>
    public abstract class BaseHandler : AHttpHandler
    {
        #region IBllBaseHandler抽象方法

        /// <summary>
        /// 添加操作
        /// </summary>
        /// <param name="responseInf"></param>
        public abstract void Add(ResponseInf responseInf);

        /// <summary>
        /// 修改操作
        /// </summary>
        /// <param name="responseInf"></param>
        public abstract void Update(ResponseInf responseInf);

        /// <summary>
        /// 删除操作
        /// </summary>
        /// <param name="responseInf"></param>
        public abstract void Delete(ResponseInf responseInf);

        /// <summary>
        /// 查询操作
        /// </summary>
        /// <param name="responseInf"></param>
        public abstract void Select(ResponseInf responseInf);

        /// <summary>
        /// 其他的操作
        /// </summary>
        /// <param name="responseInf"></param>
        public abstract void DoOther(ResponseInf responseInf);

        #endregion

        public override void ProcessRequest()
        {

            ResponseInf responseInf = new ResponseInf();
            try
            {
                string op_type = Request.Params["op_type"];

                switch (op_type)
                {

                    case "Add":
                        //可以添加记录日志
                        this.Add(responseInf);
                        break;
                    case "Update":
                        //可以添加记录日志
                        this.Update(responseInf);
                        break;
                    case "Delete":
                        //可以添加记录日志
                        this.Delete(responseInf);
                        break;
                    case "Select":
                        this.Select(responseInf);
                        break;
                    default:
                        DoOther(responseInf);
                        break;
                }
            }
            catch (MyException myEx)
            {
                //自定义异常不需要记录日志
                responseInf.MessageCode = "erro";
                responseInf.Message = myEx.ShowMessage;
            }
            catch (NotImplementedException NotImplementedex)
            {
                //操作类没有实现
                responseInf.MessageCode = "erro";
                responseInf.Message = "没有你需要执行的操作！" + NotImplementedex.Message;
            }
            catch (DivideByZeroException)
            {
                responseInf.MessageCode = "erro";
                responseInf.Message = "请在基本属性设置中填写各个县,镇,村的人口数";
            }
            catch (Exception ex)
            {
                //系统日志异常需要记录
                if (ex.InnerException is MyException)
                {
                    responseInf.MessageCode = "erro";
                    responseInf.Message = ex.InnerException.Message;
                }
                else
                {
                    responseInf.MessageCode = "erro";
                    responseInf.Message = ex.Message;
                    AppLogger.Log("程序出现异常", ex);
                }
            }
            
            finally
            {
                if (string.IsNullOrEmpty(responseInf.FormatString))
                {
                    Response.Write(JsonSerializer.Serialize(responseInf, "yyyy-MM-dd HH:mm:ss"));
                }
                else
                {
                    Response.Write(JsonSerializer.Serialize(responseInf, responseInf.FormatString));
                }
            }
        }

       
    }

    /// <summary>
    /// 装饰 page,以方便快速定位到handler
    /// </summary>
    [AttributeUsage(AttributeTargets.Class)]
    public class HandlerAttribute : Attribute
    {
        public HandlerAttribute(Type ty)
        {
            
        }

        protected Type ClassType { get; set; }
    }
}
